#include "playscene.h"
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QPushButton>
#include <QTimer>
#include <QPainter>
#include <QLabel>
#include <QFont>
#include <QDebug>
#include <QPropertyAnimation>
#include <QSound>
#include "mypushbutton.h"
#include "mycoin.h"
#include "dataconfig.h"
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{

//}


PlayScene::PlayScene(int levelNum)
{
    this->levelIndex=levelNum;
    //场景初始化
    //设置固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/resource/Coin0001.png"));
    //设置标题
    QString titleStr=QString("第%1关").arg(levelNum);
    this->setWindowTitle(titleStr);
    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);
    //创建开始菜单
    QMenu *startMenu=bar->addMenu("开始");
    //创建退出功能
    QAction *quitAction=startMenu->addAction("退出");
    //实现退出功能
    connect(quitAction,&QAction::triggered,this,[=]()
    {
        this->close();
    });
    //返回按钮
    MyPushButton *backBtn=new MyPushButton(":/resource/BackButton.png",":/resource/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    //返回音效
    QSound *backSound=new QSound(":/resource/BackButtonSound.wav",this);
    //实现返回按钮功能
    connect(backBtn,&MyPushButton::clicked,this,[=]()
    {
       backSound->play();
       QTimer::singleShot(500,this,[=]
       {
           emit this->chooseSceneBack();
       }) ;
    });
    //显示当前关卡
    QLabel *levelLabel=new QLabel;
    levelLabel->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString levelStr=QString("Level:%1").arg(this->levelIndex);
    levelLabel->setFont(font);
    levelLabel->setText(levelStr);
    levelLabel->setGeometry(QRect(30,this->height()-50,120,50));
    //设置胜利图片
    QLabel *winLabel=new QLabel;
    QPixmap winPix;
    winPix.load(":/resource/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,winPix.width(),winPix.height());
    winLabel->setPixmap(winPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-winLabel->width())*0.5,-winPix.height());
    //初始化此关卡的金币配置二位数组
    dataConfig config;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
    }
    //创建金币框架
    //音效
    QSound *flipSound=new QSound(":/resource/ConFlipSound.wav",this);//翻金币音效
    QSound *winSound=new QSound(":/resource/LevelWinSound.wav");
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            QLabel *coinBgLabel=new QLabel;
            coinBgLabel->setGeometry(0,0,50,50);
            coinBgLabel->setPixmap(QPixmap(":/resource/BoardNode.png"));
            coinBgLabel->setParent(this);
            coinBgLabel->move(57+i*50,200+j*50);
            //创建金币
            QString coinStr;
            if(this->gameArray[i][j]==1)
            {
                coinStr=":/resource/Coin0001.png";
            }
            else
            {
                coinStr=":/resource/Coin0008.png";
            }

            MyCoin *coin=new MyCoin(coinStr);
            coin->setParent(this);
            coin->move(59+i*50,204+j*50);
            coin->posX=i;
            coin->posY=j;
            coin->flag=this->gameArray[i][j];//1正面，0反面
            //将金币放入二维数组方便管理
            coinBtn[i][j]=coin;
            //监听点击事件实现翻转
            connect(coin,&MyCoin::clicked,this,[=]()
            {
                flipSound->play();
                for(int i=0;i<4;i++)
                {
                    for(int j=0;j<4;j++)
                    {
                        this->coinBtn[i][j]->isWin=true;
                    }
                }

                coin->changeFlag();
                this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;
                //延时翻转
                QTimer::singleShot(100,this,[=]()
                {
                    //反转周围
                    if(coin->posX+1<=3)//周围右侧条件
                    {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY]=!this->gameArray[coin->posX+1][coin->posY];
                    }
                    if(coin->posX-1>=0)//周围左侧条件
                    {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY]=!this->gameArray[coin->posX-1][coin->posY];
                    }
                    if(coin->posY-1>=0)//周围上侧侧条件
                    {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1]=!this->gameArray[coin->posX][coin->posY-1];
                    }
                    if(coin->posY+1<=3)//周围下侧条件
                    {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1]=!this->gameArray[coin->posX][coin->posY+1];
                    }

                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            this->coinBtn[i][j]->isWin=false;
                        }
                    }

                    //判断是否胜利
                    this->isWin=true;
                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            if(coinBtn[i][j]->flag==false)
                            {
                                this->isWin=false;
                                break;
                            }
                        }
                    }

                    if(this->isWin==true)
                    {
                        //
                        qDebug()<<"游戏胜利";
                        //将所有的按钮胜利标志改为true
                        for(int i=0;i<4;i++)
                        {
                            for(int j=0;j<4;j++)
                            {
                                coinBtn[i][j]->isWin=true;
                            }
                        }

                        //胜利图片出现
                        QPropertyAnimation *animation=new QPropertyAnimation(winLabel,"geometry");
                        //设置时间间隔
                        animation->setDuration(1000);
                        //设置开始位置
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        //设置结束位置
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+180,winLabel->width(),winLabel->height()));
                        //设置曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        //执行动画
                        animation->start();
                        winSound->play();


                    }
                });


            });

        }
    }

}

void PlayScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    //背景图1
    pix.load(":/resource/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //背景图2
    pix.load(":/resource/Title.png");
    //缩放一下
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);
}
